#include "pch.h"
#include "GameClock.h"


GameClock::GameClock()
{
	if (!QueryPerformanceFrequency(&Frequency))
	{
		throw ref new Platform::FailureException();
	}
	Reset();
}

void GameClock::Reset()
{
	Update();
	StartTime = CurrentTime;
	Total = 0.0f;
	Delta = 1.0f / 60.0f;
}

void GameClock::Update()
{
	if (!QueryPerformanceCounter(&CurrentTime))
	{
		throw ref new Platform::FailureException();
	}

	Total = static_cast<float>(
		static_cast<double>(CurrentTime.QuadPart - StartTime.QuadPart) /
		static_cast<double>(Frequency.QuadPart)
		);

	if (LastTime.QuadPart == StartTime.QuadPart)
	{
		// If the timer was just reset, report a time delta equivalent to 60Hz frame time.
		Delta = 1.0f / 60.0f;
	}
	else
	{
		Delta = static_cast<float>(
			static_cast<double>(CurrentTime.QuadPart - LastTime.QuadPart) /
			static_cast<double>(Frequency.QuadPart)
			);
	}

	LastTime = CurrentTime;
}

float GameClock::GetTotal()
{
	return Total;
}

float GameClock::GetDelta()
{
	return Delta;
}
